<?php
use think\facade\Db;
 
class confirmAdd{
    public function TODO($ws,$frame,$fd){
        $param = (array)json_decode($frame->data); 
 
        $isEmpty = Db::name('user')->where("uuid",$param['toMember'])->find(); 
         
        if(!empty($isEmpty)){ 
            $isFriend = Db::name('friend')->where("toMember",$param['memberTo'])->where("memberTo",$param['memberTo'])->find();  
            if($isFriend){
                $return = [
                    'status'=>0,
                    'msg'=>'您已是该用户好友，无需申请',
                    'act'=>'confirmAdd',
                    'fd'=>$fd,
                ];
            }else{
                $isApply = Db::name('friend_apply')
                
                            ->where('status',0)
                            
                            ->where('memberTo',$param['memberTo'])
                            
                            ->where("toMember",$isEmpty['username'])
                            
                            ->find();    
                if($isApply){
                      $return = [
                        'status'=>1,
                        'msg'=>'已有申请，请勿重复申请',
                        'data'=>'', 
                        'act'=>'confirmAdd',
                        'fd'=>$fd,
                    ];
                }else{
                    $collname = 'friend_apply';
                    
                    $data = [
                            "toMember"      =>  $isEmpty['username'],
                            "memberTo"      =>  $param['memberTo'],
                            "create_time"   =>  time(),
                            "status"        =>  0,
                            "to_uuid"       =>  $isEmpty['uuid'],
                            "uuid_to"       =>  $param['uuid']
                        ];  
                    $isApply =  Db::name('friend_apply')->insert($data); 
                    if($isEmpty['fd'] !=''){
                        $data = [
                            'status'=>1,
                            'msg'=>'您有好友添加，请查看申请记录',
                            'data'=>$data, 
                            'act'=>'NewApply',
                            'fd'=>$isEmpty['fd'],
                        ];
                        $ws->push($isEmpty['fd'],json_encode($data));
                    }
                    $return = [
                        'status'=>1,
                        'msg'=>'已提交申请，请等待回复',
                        'data'=>$isApply, 
                        'act'=>'confirmAdd',
                        'fd'=>$fd,
                    ];
                    
                }
            }
             
        }else{
            $return = [
                'status'=>0,
                'msg'=>'该账户不存在',
                'data'=>'', 
                ];
        }
        return $return;
    }
}